package com.shuqi.activity.adapter;

import java.util.List;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuqi.activity.BookSearchParentActivity;
import com.shuqi.activity.BookSearchResultActivity;
import com.shuqi.controller.R;
import com.shuqi.model.bean.SearchAssociateInfo;

/**
 * 搜索联想适配器
 * 
 * @author linyuren
 * 
 */
public class SearchPopupWindowAdapter extends BaseAdapter {

    /** 存储数据集合 */
    private List<SearchAssociateInfo> list;
    private LayoutInflater inflater;
    /** false=本地；true=在线； */
    private boolean isOnline;
    private Activity activity;

    public SearchPopupWindowAdapter(Activity activity, List<SearchAssociateInfo> list,
            boolean isline) {
        this.inflater = LayoutInflater.from(activity);
        this.activity = activity;
        this.list = list;
        this.isOnline = isline;
    }

    public void setList(List<SearchAssociateInfo> list) {
        this.list = list;
    }

    public List<SearchAssociateInfo> getList() {
        return list;
    }

    public void setItemOnclick(String itemOnclick) {
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null && list.get(position) != null) {
            return list.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.item_search_history, parent, false);
        }
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.ll = (LinearLayout) view.findViewById(R.id.tv_search_ll);
            holder.tv = (TextView) view.findViewById(R.id.tv_search_info);
            holder.search_icon = (ImageView) view.findViewById(R.id.search_icon);
            view.setTag(holder);
        }
        if (isOnline) {
            if (!TextUtils.isEmpty(list.get(position).getTitle())) {
                holder.tv.setText(list.get(position).getTitle());
                holder.search_icon.setBackgroundResource(R.drawable.search_think);
            }
        } else {
            if (list.get(position).getHistroyInfo() != null) {
                holder.tv.setText(list.get(position).getHistroyInfo());
                holder.search_icon.setBackgroundResource(R.drawable.search_history);
            }
        }
        holder.ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (activity instanceof BookSearchParentActivity) {
                    if (isOnline) {
                        ((BookSearchParentActivity) activity).jumpBookSearchResultActivity(list
                                .get(position).getTitle());
                    } else {
                        ((BookSearchParentActivity) activity).jumpBookSearchResultActivity(list
                                .get(position).getHistroyInfo());
                    }
                } else if (activity instanceof BookSearchResultActivity) {
                    if (isOnline) {
                        ((BookSearchResultActivity) activity).refreshLoadUrl(list.get(position)
                                .getTitle());
                    } else {
                        ((BookSearchResultActivity) activity).refreshLoadUrl(list.get(position)
                                .getHistroyInfo());
                    }
                }
            }
        });
        return view;
    }

    private static class Holder {
        public LinearLayout ll;
        public TextView tv;
        public ImageView search_icon;
    }
}
